package com.dianda.generic.commons.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author RenJun
 */
public class DateUtils {
	public static String getWeekDesc(Calendar cal) {
		int i = cal.get(Calendar.DAY_OF_WEEK) - 1;
		switch (i) {
		case 1:
			return "星期一";
		case 2:
			return "星期二";
		case 3:
			return "星期三";
		case 4:
			return "星期四";
		case 5:
			return "星期五";
		case 6:
			return "星期六";
		case 0:
			return "星期日";
		}
		return "";
	}

	/**
	 * 日期转字符串
	 * @param d
	 * @param fmt
	 * @return
	 */
	public static String format(Date d, String fmt) {
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		return sdf.format(d);
	}

	/**
	 * 字符串转日期
	 * @param str
	 * @param fmt
	 * @return
	 */
	public static Date parseDate(String str,String fmt) {
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		Date ret = null;
		try {
			ret = sdf.parse(str);
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return ret;
	}

	public static Calendar begin(Calendar c) {
		c.set(Calendar.HOUR, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c;
	}

	public static Calendar end(Calendar c) {
		c.set(Calendar.HOUR, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		return c;
	}

	public static Date[] getWeekBeginAndEnd(Calendar cal) {
		int i = cal.get(Calendar.DAY_OF_WEEK);
		Date[] d = new Date[2];
		cal.add(Calendar.DATE, -(i - 2));
		d[0] = begin(cal).getTime();
		cal.add(Calendar.DATE, 6);
		d[1] = end(cal).getTime();
		return d;
	}

	/**
	 * 转换日期格式 
	 * 如：20160606转换成2016-06-06
	 * @param str
	 * @return
	 */
	public static String str2DateString(String str) {
		String str_year = str.substring(0, 4);
		String str_mouth = str.substring(4, 6);
		String str_day = str.substring(6, 8);
		return str_year + "-" + str_mouth + "-" + str_day;
	}
	
	/**
	 * 转换时间格式
	 * 如：2016转换成20:16
	 * @param str
	 * @return
	 */
	public static String str2TimeString(String str) {
		String str_hour = str.substring(0, 2);
		String str_min = str.substring(2, 4);
		return str_hour + ":" + str_min;
	}

	/**
	 * 日期加天数
	 * 
	 * @param specifiedDay
	 *            日期
	 * @param m
	 *            加几天
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay, Integer m) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);
		} catch (Exception e) {
			// e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + m);
		String dayAfter = new SimpleDateFormat("yyyyMMdd").format(c.getTime());
		return dayAfter;
	}

	public static String getSpecifiedDayBefore(String specifiedDay, Integer m) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (Exception e) {
			// e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - m);
		String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}

	/**
	 * 计算两个日期之间的日期，并包含自己。
	 * 
	 * @param beginTime
	 * @param endTime
	 * @param format
	 *            日期格式
	 * @return
	 */
	public static List<String> getBetweenDateAndDate(String beginTime, String endTime, String format) {
		DateFormat FORMATTER = new SimpleDateFormat(format);
		List<String> list = null;
		try {
			Calendar startDay = Calendar.getInstance();
			Calendar endDay = Calendar.getInstance();
			startDay.setTime(FORMATTER.parse(beginTime));
			endDay.setTime(FORMATTER.parse(endTime));
			// 给出的日期开始日比终了日大则不执行打印
			list = new ArrayList<String>();
			list.add(beginTime);
			if (!beginTime.equals(endTime)) {
				if (startDay.compareTo(endDay) <= 0) {
					// 现在打印中的日期
					Calendar currentPrintDay = startDay;
					while (true) {
						// 日期加一
						currentPrintDay.add(Calendar.DATE, 1);
						// 日期加一后判断是否达到终了日，达到则终止打印
						if (currentPrintDay.compareTo(endDay) == 0) {
							break;
						}
						list.add(FORMATTER.format(currentPrintDay.getTime()));
					}
					list.add(endTime);
				}
			}
			// for (String s : list){
			// System.out.println(s);
			// }
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return list;
	}

	/**
	 * 比较当前日期是否在两者之间，当前日期默认系统日期
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareDate(String date1, String date2) {
		boolean flag = false;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String currentDateStr = format(new Date(), "yyyy-MM-dd");
		try {
			Date currentDate = df.parse(currentDateStr);
			Date dt1 = df.parse(date1);
			Date dt2 = df.parse(date2);
			if (currentDate.getTime() >= dt1.getTime() && currentDate.getTime() <= dt2.getTime()) {
				flag = true;
			} else {
				flag = false;
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return flag;
	}
	
	
	/**
	 * 比较一个日期是否在两者之间，自定义输入日期
	 * @param startDate
	 * @param endDate
	 * @param inputDate 自定义日期
	 * @return
	 */
	public static boolean compareDate(String startDate, String endDate,String inputDate) {
		boolean flag = false;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date currentDate = df.parse(inputDate);
			Date dt1 = df.parse(startDate);
			Date dt2 = df.parse(endDate);
			if (currentDate.getTime() >= dt1.getTime() && currentDate.getTime() <= dt2.getTime()) {
				flag = true;
			} else {
				flag = false;
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 比较当前日期是否在两者之间，带时间格式
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareDateTime(String date1, String date2) {
		boolean flag = false;
		DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm");
		String currentDateStr = format(new Date(), "yyyyMMdd HH:mm");
		try {
			Date currentDate = df.parse(currentDateStr);
			Date dt1 = df.parse(date1);
			Date dt2 = df.parse(date2);
			if (currentDate.getTime() >= dt1.getTime() && currentDate.getTime() <= dt2.getTime()) {
				flag = true;
			} else {
				flag = false;
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 计算两个日期时间的相差天数、小时、分钟
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static String getDatePoor(String startDate, String endDate) {
		Date sDate = null;
		Date eDate = null;
		String returnDate = "";
		try {
			sDate = new SimpleDateFormat("yyyyMMdd HH:mm").parse(startDate);
			DateFormat.getInstance().format(sDate);
			eDate = new SimpleDateFormat("yyyyMMdd HH:mm").parse(endDate);
			DateFormat.getInstance().format(eDate);

			long nd = 1000 * 24 * 60 * 60;
			long nh = 1000 * 60 * 60;
			long nm = 1000 * 60;
			long diff = eDate.getTime() - sDate.getTime();
			long day = diff / nd;
			long hour = diff % nd / nh;
			long min = diff % nd % nh / nm;

			returnDate = day + "天" + hour + "小时" + min + "分钟";

		} catch (Exception e) {
			// e.printStackTrace();
		}
		return returnDate;
	}

	/**
	 * 计算两个日期时间的相差的分钟
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static long getMinutesByDateTime(String startDate, String endDate) {
		Date sDate = null;
		Date eDate = null;
		long returnDate = 0L;
		try {
			sDate = new SimpleDateFormat("yyyyMMdd HH:mm").parse(startDate);
			DateFormat.getInstance().format(sDate);
			eDate = new SimpleDateFormat("yyyyMMdd HH:mm").parse(endDate);
			DateFormat.getInstance().format(eDate);

			returnDate = eDate.getTime() - sDate.getTime();

		} catch (Exception e) {
			// e.printStackTrace();
		}
		return returnDate / 1000 / 60;
	}

	/**
	 * 比较两个时间的大小
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static boolean compareTime(String time1, String time2) {
		Date sDate = null;
		Date eDate = null;
		long returnDate = 0L;
		boolean f = false;
		try {
			sDate = new SimpleDateFormat("HH:mm").parse(time1);
			DateFormat.getInstance().format(sDate);
			eDate = new SimpleDateFormat("HH:mm").parse(time2);
			DateFormat.getInstance().format(eDate);

			returnDate = eDate.getTime() - sDate.getTime();

			if (returnDate < 0) {
				f = true;
			}

		} catch (Exception e) {
			// e.printStackTrace();
		}
		return f;
	}

	public static Calendar setCalendar(String date) {
		Calendar c = Calendar.getInstance();
		String strs[] = date.split("-");
		c.set(Integer.valueOf(strs[0]), Integer.valueOf(strs[1]) - 1, Integer.valueOf(strs[2]));
		return c;
	}

	public static Calendar getAfter(Calendar c) {
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);
		return c;
	}

	public static String getDateStr(Calendar c) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(c.getTime());
	}

	public static int compareDate(Calendar c1, Calendar c2) {
		return c1.compareTo(c2);
	}

	/**
	 * 获取当年的第一天
	 * 
	 * @return
	 */
	public static Date getCurrYearFirst() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearFirst(currentYear);
	}

	/**
	 * 获取当年的最后一天
	 * 
	 * @return
	 */
	public static Date getCurrYearLast() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearLast(currentYear);
	}

	/**
	 * 获取某年第一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getYearFirst(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}

	/**
	 * 获取某年最后一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getYearLast(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();

		return currYearLast;
	}

	/**
	 * 当前年的开始时间，即2012-01-01 00:00:00
	 * 
	 * @return
	 */
	public Date getCurrentYearStartTime() {
		SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.MONTH, 0);
			c.set(Calendar.DATE, 1);
			now = longSdf.parse(shortSdf.format(c.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前年的结束时间，即2012-12-31 23:59:59
	 * 
	 * @return
	 */
	public Date getCurrentYearEndTime() {
		SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.MONTH, 11);
			c.set(Calendar.DATE, 31);
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获取前/后半年的开始时间
	 * 
	 * @return
	 */
	public static Date getHalfYearStartTime() {
		SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 0);
			} else if (currentMonth >= 7 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 6);
			}
			c.set(Calendar.DATE, 1);
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;

	}

	/**
	 * 获取前/后半年的结束时间
	 * 
	 * @return
	 */
	public static Date getHalfYearEndTime() {
		SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 5);
				c.set(Calendar.DATE, 30);
			} else if (currentMonth >= 7 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 11);
				c.set(Calendar.DATE, 31);
			}
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前季度的开始时间，即2012-01-1 00:00:00
	 * 
	 * @return
	 */
	public Date getCurrentQuarterStartTime() {
		SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 3)
				c.set(Calendar.MONTH, 0);
			else if (currentMonth >= 4 && currentMonth <= 6)
				c.set(Calendar.MONTH, 3);
			else if (currentMonth >= 7 && currentMonth <= 9)
				c.set(Calendar.MONTH, 4);
			else if (currentMonth >= 10 && currentMonth <= 12)
				c.set(Calendar.MONTH, 9);
			c.set(Calendar.DATE, 1);
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前季度的结束时间，即2012-03-31 23:59:59
	 * 
	 * @return
	 */
	public Date getCurrentQuarterEndTime() {
		SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 3) {
				c.set(Calendar.MONTH, 2);
				c.set(Calendar.DATE, 31);
			} else if (currentMonth >= 4 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 5);
				c.set(Calendar.DATE, 30);
			} else if (currentMonth >= 7 && currentMonth <= 9) {
				c.set(Calendar.MONTH, 8);
				c.set(Calendar.DATE, 30);
			} else if (currentMonth >= 10 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 11);
				c.set(Calendar.DATE, 31);
			}
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获得本周的第一天，周一
	 * 
	 * @return
	 */
	public Date getCurrentWeekDayStartTime() {
		SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		try {
			int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
			c.add(Calendar.DATE, -weekday);
			c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c.getTime();
	}

	/**
	 * 获得本周的最后一天，周日
	 * 
	 * @return
	 */
	public Date getCurrentWeekDayEndTime() {
		SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		try {
			int weekday = c.get(Calendar.DAY_OF_WEEK);
			c.add(Calendar.DATE, 8 - weekday);
			c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c.getTime();
	}

	/**
	 * 判断当前日期是否在该时间之前
	 * @param date 格式日期+时间
	 * @return
	 */
	public static boolean isDateBefore(String date){
		try {
			Date d = new Date();
			DateFormat df = DateFormat.getDateTimeInstance();
			return d.before(df.parse(date));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 判断当前日期是否在该时间之后
	 * @param date 格式日期+时间
	 * @return
	 */
	public static boolean isDateAfter(String date){
		try {
			Date d = new Date();
			DateFormat df = DateFormat.getDateTimeInstance();
			return d.after(df.parse(date));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static void main(String[] args) {
		System.out.println(compareDate("2016-06-03", "2016-06-28","2016-06-30"));
	}

}
